package org.taobao.ibatiside.ui.action;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.datatools.connectivity.IConnectionProfile;
import org.eclipse.datatools.connectivity.ProfileManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.wizard.WizardDialog;
import org.osgi.service.prefs.BackingStoreException;
import org.taobao.ibatiside.core.filemodel.IbatisMapperFile;
import org.taobao.ibatiside.ui.Activator;
import org.taobao.ibatiside.ui.pref.BatisProjectSettingPage;
import org.taobao.ibatiside.ui.util.SelectConnectionProfileDialog;
import org.taobao.ibatiside.ui.wizard.OverviewNewStatementWizard;

public class OverviewOpenInWizardAction extends OverviewAction {
	public static final String ID="org.taobao.ibatiside.ui.action.iov.stmtinwizard";
	public OverviewOpenInWizardAction() {
		super();
		setId(ID);
		setText("New Statement");
		setImageDescriptor(Activator.getIcon("op16/wizardstmt.png"));
	}
	@Override
	public void run() {
	
		IbatisMapperFile mapper_file = (IbatisMapperFile) getSelectObject();
		IConnectionProfile connection_profile = getExpectedConnection(mapper_file);
		if (connection_profile == null){
			return;
		}
		IStatus connect = connection_profile.connect();
		if (!connect.isOK()){
			MessageDialog.openWarning(viewer.getControl().getShell(), "Connection Wrong!", connect.getMessage());
			return;
		}
		OverviewNewStatementWizard wizard = new OverviewNewStatementWizard();
		wizard.setWindowTitle("Create Statement from iBATIS overview");
		wizard.getModel().setConnectionProfile(connection_profile);
		wizard.getModel().setConfigurationFile((IFile) mapper_file.getParent().getResource());
		wizard.getModel().setSqlmapFile(mapper_file.getSqlmapFile());
		wizard.getModel().setOpSql("");
		WizardDialog dialog = new WizardDialog(viewer.getControl().getShell(),wizard);
		dialog.setPageSize(700,600);
		if (dialog.open() == WizardDialog.OK) {
			postWizard(wizard);
		}
	}
	private void postWizard(OverviewNewStatementWizard wizard) {
		
	}
	private IConnectionProfile getExpectedConnection(
			IbatisMapperFile mapper_file) {
		ProjectScope scope = new ProjectScope(mapper_file.getResource().getProject());
		IEclipsePreferences preference = scope.getNode(Activator.PLUGIN_ID);
		String stored_profile_name = preference.get(BatisProjectSettingPage.CONNECTION,"");
		if(StringUtils.isBlank(stored_profile_name)){
			SelectConnectionProfileDialog dialog = new SelectConnectionProfileDialog(viewer.getControl().getShell());
			if (dialog.open() == SelectConnectionProfileDialog.OK){
				IConnectionProfile returnProfile = dialog.getReturnProfile();
				if (returnProfile != null){
					preference.put(BatisProjectSettingPage.CONNECTION, returnProfile.getName());
					try {
						preference.flush();
					} catch (BackingStoreException e) {
						e.printStackTrace();
					}
				}
				return returnProfile;
			}else{
				return null;
			}
		}else{
			return ProfileManager.getInstance().getProfileByName(stored_profile_name);
		}
	}
}
